/* Copyright (c) 2008 Google Inc.
*  Copyright (c) 2009 Pierre Henri Kuate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace org.opensocial.client
{
    /// <summary> An object which represents a simple URL. Once an object is instantiated,
    /// path components and parameters can be added. Once all of the elements
    /// are in place, the object can be serialized into a string. This class
    /// is used by internal classes and not by clients directly.
    /// </summary>
    /// <author>  Jason Cooper
    /// </author>
    public class OpenSocialUrl
    {
        private readonly string baseUrl;
        private readonly System.Collections.Generic.IList<string> components;
        private readonly System.Collections.Generic.IDictionary<string, string> queryStringParameters;

        public OpenSocialUrl(string baseUrl)
        {
            this.baseUrl = baseUrl;
            components = new System.Collections.Generic.List<string>();
            queryStringParameters = new System.Collections.Generic.Dictionary<string, string>();
        }

        public System.Collections.Generic.IDictionary<string, string> QueryStringParameters
        {
            get { return queryStringParameters; }
        }

        /// <summary>
        ///  Adds passed String to the path component queue.
        /// </summary>
        /// <param name="component">Path component to add
        /// </param>
        public void addPathComponent(string component)
        {
            components.Add(component);
        }

        /// <summary> Creates a new entry in queryStringParameters Map with the passed key and
        /// value; used for adding URL parameters such as oauth_signature and the
        /// various other OAuth parameters that are required in order to submit a
        /// signed request.
        /// </summary>
        /// <param name="key">Parameter name
        /// </param>
        /// <param name="value">Parameter value
        /// </param>
        public void addQueryStringParameter(string key, string value)
        {
            QueryStringParameters.Add(key, value);
        }

        /// <summary> Returns a String representing the serialized URL including the base
        /// followed by any path components added to the path component queue
        /// and, last but not least, appending any query string parameters as
        /// name-value pairs after the full path.
        /// </summary>
        public override string ToString()
        {
            var s = new System.Text.StringBuilder(baseUrl);

            foreach (string pathComponent in components)
            {
                if (s[s.Length - 1] != '/')
                {
                    s.Append("/");
                }
                s.Append(pathComponent);
            }

            string connector = "?";
            foreach (var e in QueryStringParameters)
            {
                s.Append(connector);
                s.Append(e.Key);
                s.Append("=");
                s.Append(e.Value);
                connector = "&";
            }

            return s.ToString();
        }

        public System.Uri ToUri()
        {
            return new System.Uri(ToString());
        }
    }
}